import os
import base64
from openai import OpenAI

# 读取本地图片并转换为 Base64
def image_to_base64(file_path):
    with open(file_path, "rb") as image_file:
        return base64.b64encode(image_file.read()).decode("utf-8")

# 替换为你的本地图片路径
local_image_path = "input_files/测试发票-灵算-专票.png"
base64_image = image_to_base64(local_image_path)

client = OpenAI(
    api_key=os.getenv("DASHSCOPE_API_KEY"),
    base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",
)

completion = client.chat.completions.create(
    model="qwen-vl-plus-2025-01-25",
    messages=[
        {
            "role": "user",
            "content": [
                {"type": "text", "text": "这是什么"},
                {
                    "type": "image_url",
                    "image_url": {
                        "url": f"data:image/jpeg;base64,{base64_image}"  # 使用 Base64 数据
                    },
                },
            ],
        }
    ],
)

print(completion.model_dump_json())